import { getCurrentEmissionState } from "lib/store/GlobalStore"
import InAppReview from "react-native-in-app-review"

// Whether we have requested during the current session or not.
let hasRequested = false
/**
 * Callback for when user has meaningfully interacted with the app (eg: followed an artist).
 * This method is designed to be called often, and it encapsulates all logic necessary for
 * deciding whether or not to prompt the user for an app rating.
 */
export async function userHadMeaningfulInteraction() {
  const launchCount = getCurrentEmissionState().launchCount

  // We choose to ask the user on their second session, as well as their 22nd, 42nd, etc.
  // Apple will only ever ask the user to rate the app 3 times in a year, and we want to
  // space out how often we ask for a rating so users have gotten a sense of the app's value.
  if (launchCount % 20 === 2) {
    if (!hasRequested) {
      hasRequested = true
      try {
        await InAppReview.RequestInAppReview()
      } catch (error) {
        console.log("something went wrong while prompting for review")
        console.log(error)
      }
    }
  }
}
